package com.hongtech.modules.app.controller;

import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.modules.app.dto.UserDTO;
import com.hongtech.modules.app.entity.UserEntity;
import com.hongtech.modules.app.form.RegisterForm;
import com.hongtech.modules.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 注册
 */
@RestController
@RequestMapping("/app")
@Api(tags = "APP注册接口")
public class AppRegisterController {

    @Autowired
    private UserService userService;

    @PostMapping("register")
    @ApiOperation("注册")
    public AjaxResultVo register(@RequestBody RegisterForm form) {
        UserDTO userDTO = userService.queryByMobile(form.getMobile());
        if (ObjectUtils.isEmpty(userDTO)) {
            UserEntity user = new UserEntity();
            user.setMobile(form.getMobile());
            user.setUsername(form.getMobile());
            user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
            user.setHeadPic("https://news.mydrivers.com/Img/20110518/04481549.png");
            userService.save(user);
            return AjaxResultVo.success();
        } else {
            return AjaxResultVo.error("该手机号已注册！");
        }
    }

}
